﻿using System;
using System.Data;
using System.Xml;
using System.Collections;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Configuration;

namespace DataAccess.Operate
{
    public class DA_Login_Inherit : SqlInteractionBase
    {

        #region 身份验证
        public bool Login_Check(string sLoginID, string sPassword, out bool bLogin_IsBusiness, out int iLogin_ID)
        {
            KDHR.Service Service = new KDHR.Service();
            string sKDHR;
            bLogin_IsBusiness = false;
            iLogin_ID = 0;

            sKDHR = Service.getEmployeeStatus(sLoginID);
            if (sKDHR == "")
            {
                return false;
            }

            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[pr_EC_Login_Check]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;
            cmdToExecute.Connection = _mainConnection;
            bool isReturn = false;

            try
            {
                cmdToExecute.Connection = _mainConnection;
                cmdToExecute.Parameters.Add(new SqlParameter("@guidLogin_KDHRID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, new Guid(sKDHR)));
                cmdToExecute.Parameters.Add(new SqlParameter("@sLogin_Password", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, sPassword));
                cmdToExecute.Parameters.Add(new SqlParameter("@bLogin_IsBusiness", SqlDbType.Bit, 1, ParameterDirection.Output, false, 0, 0, "", DataRowVersion.Proposed, bLogin_IsBusiness));
                cmdToExecute.Parameters.Add(new SqlParameter("@iLogin_ID", SqlDbType.Int, 8, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, iLogin_ID));

                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Open();

                    cmdToExecute.Transaction = _mainConnection.BeginTransaction();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                _rowsAffected = cmdToExecute.ExecuteNonQuery();

                if (_rowsAffected == 1)
                {
                    bLogin_IsBusiness = Convert.ToBoolean(cmdToExecute.Parameters["@bLogin_IsBusiness"].Value.ToString());
                    iLogin_ID = Convert.ToInt32(cmdToExecute.Parameters["@iLogin_ID"].Value.ToString());
                    isReturn = true;
                }
                if (_mainConnectionIsCreatedLocal)
                {
                    cmdToExecute.Transaction.Commit();
                }

                return isReturn;
            }
            catch (Exception ex)
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    cmdToExecute.Transaction.Rollback();
                }
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Close();
                }

                cmdToExecute.Dispose();
            }
        }
        #endregion

        #region 退出系统
        public void Logout(string sLoginID, int iSystemID)
        {
            string sSql = "update DB_EcSystemPowerManage.dbo.t_System_Using set Login_LogoutTime=getdate() where Using_LoginID=" + sLoginID + " and Using_SystemID=" + iSystemID;

            SqlCommand cmdToExecute = new SqlCommand();
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);
            DataSet ds = new DataSet();
            cmdToExecute.CommandText = sSql;
            cmdToExecute.CommandType = CommandType.Text;

            try
            {
                cmdToExecute.Connection = _mainConnection;

                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                adapter.Fill(ds);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Close();
                }

                cmdToExecute.Dispose();

                adapter.Dispose();
            }
        }
        #endregion

        #region 取某人的权限
        public DataTable getUserPurview(int iLogin)
        {
            string strSQL = "select PermissionForECSM_Module_Purview from DB_EcSystemPowerManage.dbo.t_User_PermissionForECSM where PermissionForECSM_LoginID=" + iLogin + "";
            SqlCommand cmdToExecute = new SqlCommand();
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);
            DataSet ds = new DataSet();
            cmdToExecute.CommandText = strSQL;
            cmdToExecute.CommandType = CommandType.Text;

            try
            {
                cmdToExecute.Connection = _mainConnection;

                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                adapter.Fill(ds);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Close();
                }

                cmdToExecute.Dispose();

                adapter.Dispose();
            }

            return ds.Tables[0];
        }
        #endregion

        #region 修改密码
        public bool changePassword(string sKDHRID, string sPassword)
        {
            string sSql = "update DB_EcSystemPowerManage.dbo.t_System_Login set Login_Password='" + sPassword + "' where Login_KDHRID='" + sKDHRID + "'";

            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = sSql;
            cmdToExecute.CommandType = CommandType.Text;

            try
            {
                cmdToExecute.Connection = _mainConnection;

                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }
                cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Close();
                }

                cmdToExecute.Dispose();


            }
        }
        #endregion

        /// <summary>
        /// 取所属组别
        /// </summary>
        /// <param name="iLogin"></param>
        /// <returns></returns>
        public DataTable getUserBuilding(int iLogin)
        {
            string strSQL = "select User_BuildingForECSM_BuildingID from DB_EcSystemPowerManage.dbo.t_User_BuildingForECSM where User_BuildingForECSM_LoginID=" + iLogin + "";
            return RunSQL(strSQL).Tables[0];
        }


        #region 密码修改
        public bool Pwd_Change(string sKDHRID, string sPassword, bool bLogin_IsBusiness)
        {
            AppSettingsReader _configReader = new AppSettingsReader();
            SqlDataAdapter dsCommand = new SqlDataAdapter();

            try
            {
                dsCommand.SelectCommand = new SqlCommand();
                dsCommand.SelectCommand.Connection = new SqlConnection(_configReader.GetValue("Main.ConnectionString1", typeof(string)).ToString());

                dsCommand.SelectCommand.CommandText = "dbo.[pr_t_Login_Update]";
                dsCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
                dsCommand.SelectCommand.CommandTimeout = 0;
                dsCommand.SelectCommand.Parameters.Add(new SqlParameter("@guidLogin_KDHRID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, new Guid(sKDHRID)));
                dsCommand.SelectCommand.Parameters.Add(new SqlParameter("@sLogin_Password", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, sPassword));
                dsCommand.SelectCommand.Parameters.Add(new SqlParameter("@bLogin_IsBusiness", SqlDbType.Bit, 1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, bLogin_IsBusiness));

                if (dsCommand.SelectCommand.Connection.State != ConnectionState.Open)
                    dsCommand.SelectCommand.Connection.Open();

                _rowsAffected = dsCommand.SelectCommand.ExecuteNonQuery();

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (dsCommand != null)
                {
                    if (dsCommand.SelectCommand != null)
                    {
                        if (dsCommand.SelectCommand.Connection != null)
                            dsCommand.SelectCommand.Connection.Dispose();
                        dsCommand.SelectCommand.Dispose();
                    }
                    dsCommand.Dispose();
                    dsCommand = null;
                }
            }
        }
        #endregion

    }
}
